﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using QBZ.Evaluation.Models;
using QBZ.Evaluation.Repositories;
using QBZ.Evaluation.Repositories.DataAccess;
using System;
using System.Collections.Generic;
using System.Text;

namespace QBZ.Evaluation.Domains
{                                 
    public class SkillDomain
    {
        private readonly SkillRepository _skill;
        private readonly IMemoryCache _cache;

        public SkillDomain(IMemoryCache cache, SkillRepository skill)
        {
            _skill = skill;
            _cache = cache;
        }

        public List<SkillModel> GetAll()
        {
            var result = _cache.GetOrCreate("skillModelAll", (entry) =>
            {
                entry.Priority = CacheItemPriority.NeverRemove;
                return _skill.GetAll();
            });
            return result;
        }

        public bool AddSkillParent(SkillParentModel skillParent)
        {
            return _skill.AddSkillParent(skillParent);
        }

        public bool AddSkill(SkillModel skill)
        {
            return _skill.AddSkill(skill)>0;
        }

        public List<SkillParentModel> GetSkillParents()
        {
            return _skill.GetSkillParents();
        }

        public List<SkillModel> GetSkills(int parentId = 0)
        {
            return _skill.GetSkills(parentId);
        }

        public int AddSkill()
        {
            _cache.Remove("skillModelAll");
            return 0;
        }

        public bool EditSkillParent(SkillParentModel skillParent)
        {
            return _skill.EditSkillParent(skillParent);
        }

        public bool EditSkill(SkillModel skill)
        {
            return _skill.EditSkill(skill);
        }

        public List<SkillChildModel> GetSkillDetails(int skillId)
        {
            return _skill.GetSkillDetails(skillId);
        }

        public bool EditSkillDetail(SkillChildModel skillChild)
        {
            return _skill.EditSkillDetail(skillChild);
        }

        public bool AddSkillDetail(SkillChildModel skillChild)
        {
            return _skill.AddSkillDetail(skillChild);
        }

        public bool DeleteSkillDetail(int skillDetailId)
        {
            return _skill.DeleteSkillDetail(skillDetailId);
        }

        public bool DeleteSkillParent(int skillParentId)
        {
            return _skill.DeleteSkillParent(skillParentId);
        }

        public bool DeleteSkill(int skillId)
        {
            return _skill.DeleteSkill(skillId);
        }

        public bool MoveSkill(SkillModel skillModel)
        {
            return _skill.MoveSkill(skillModel);
        }

        public bool CopySkillWithDetail(SkillModel skillModel)
        {
            var id = _skill.AddSkill(skillModel);
            if (id > 0)
            {
                var childs = _skill.GetSkillDetails(skillModel.SkillId);
                foreach(var child in childs)
                {
                    child.SkillId = id;
                }
                return _skill.AddSkillDetails(childs);
            }
            else
            {
                return false;
            }
            
        }

        public bool MoveSkillDetail(SkillChildModel skillChild)
        {
            return _skill.MoveSkillDetail(skillChild);
        }
    }
}
